﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AviumX.AviumCore.WinAPI.Controls
{
    class TextBox
    {
        /*
        * Written by: Henry, for the PearOs team.
        */

        //Controls back color.
        public Screen.ScreenX.SystemColors BackColor = Screen.ScreenX.SystemColors.White;
        public bool Enabled = true;
        //Controls font.
        public Screen.Graphics.Fonts.Font.Fonts Font = Screen.Graphics.Fonts.Font.Fonts.EnvySys;
        //Controls fore color.
        public Screen.ScreenX.SystemColors ForeColor = Screen.ScreenX.SystemColors.Black;
        //Controls border color.
        public Screen.ScreenX.SystemColors BorderColor = Screen.ScreenX.SystemColors.Black;
        bool Password = false;
        //Controls location.
        public Screen.Graphics.Vector Position = new Screen.Graphics.Vector(0, 0);
        //Controls dementions.
        public uint Width = 58;
        public uint Height = 10;
        //Controls text.
        public string Text = "TextBox1";
        //A simple undo buffer
        private string TextCleared = "";
        //Controls parent.
        public Window ParentWindow;
        public bool TestOverride = false;
        //Is the control currently selected.
        bool InFocus = false;
        string key = "";
        int StringCapacity;
        int LettersToPrint;
        #region " Textbox Handlers "
        public void Draw(WinAPI.Window parent)
        {
            ParentWindow = parent;
            Screen.Graphics.Drawing.DrawRectangle(Position, Width, Height, BorderColor);
            if (InFocus == true)
            {
                Screen.Graphics.Drawing.DrawRectangle((uint)Position.x + 1, (uint)Position.y + 1, Width - 2, Height - 2, BackColor);
            }
            else
            {
                Screen.Graphics.Drawing.DrawRectangle((uint)Position.x + 1, (uint)Position.y + 1, Width - 2, Height - 2, Screen.ScreenX.SystemColors.Gray);
            }
            #region Works out which bit of the string to print - if it's too long it prints the end like in winforms
            StringCapacity = (int)Width / 8;
            if (Text.Length > StringCapacity)
            {
                LettersToPrint = Text.Length - StringCapacity;
                if (InFocus == true)
                {
                    Screen.Graphics.Fonts.FontHandle.Print(Font, ForeColor, Text.Substring(LettersToPrint), Position.x + 1, Position.y + 1);
                }
                else
                {
                    Screen.Graphics.Fonts.FontHandle.Print(Font, Screen.ScreenX.SystemColors.DarkGray, Text.Substring(LettersToPrint), Position.x + 1, Position.y + 1);
                }
            }
            else
            {
                if (InFocus == true)
                {
                    Screen.Graphics.Fonts.FontHandle.Print(Font, ForeColor, Text, Position.x + 1, Position.y + 1);
                }
                else
                {
                    Screen.Graphics.Fonts.FontHandle.Print(Font, Screen.ScreenX.SystemColors.DarkGray, Text, Position.x + 1, Position.y + 1);
                }
            }
            #endregion
            /*
               * If its parent is infront then we can handle mouse click ect.. if not then we dont
               * handle or check for mouse clicks, either way the control still gets drawn.
               */

            if (ParentWindow.infront == true)
            {
                CheckClicks();
                if (InFocus == true)
                {
                    Update();
                }
            }
            else
            {
                /*
                 * The parent is not infront so lets reset this textbox to not be clicked on.
                 */
                InFocus = false;
            }
        }
        public void TryUpdate(Window parent)
        {
            this.ParentWindow = parent;
            if (ParentWindow.infront == true)
            {
                CheckClicks();
                if (InFocus == true)
                {
                    Update();
                }
            }
            else
            {
                /*
                 * The parent is not infront so lets reset this textbox to not be clicked on.
                 */
                InFocus = false;
            }
        }
        public void Update()
        {
            /*
             * Check the keyboard for keypresses.
             */
            key = Hardware.Keyboard.KeyBoardCreated.TheKeyBoard.Key;
            if (key == "DEL")
            {
                if (Text.Length > 0)
                {
                    Text = Text.Substring(0, Text.Length - 1);
                }
                Hardware.Keyboard.KeyBoardCreated.TheKeyBoard.ClearUpdate();
            }
            else if (key == "" || key == "UP" || key == "DOWN")
            {
            }
            else if (Hardware.Keyboard.KeyBoardCreated.TheKeyBoard.ShiftKey() == 1)
            {
                Text += key.ToUpper();
                Hardware.Keyboard.KeyBoardCreated.TheKeyBoard.ClearUpdate();
            }
            else
            {
                Text += key;
                Hardware.Keyboard.KeyBoardCreated.TheKeyBoard.ClearUpdate();
            }
        }
        private void CheckClicks()
        {
            /*
             * This handles the click event for this control.
             */
            if (ParentWindow.infront == true || TestOverride == true)
            {
                if (Hardware.Mouse.Cursor.CheckMouseLeftClick(Position.x, Position.y, (int)Width, (int)Height) == true)
                {
                    InFocus = true;
                }
                else
                {
                    if (Hardware.Mouse.Cursor.Mouse.TheMouse.Buttons == Cosmos.Hardware.Mouse.MouseState.Left)
                    {
                        InFocus = false;
                    }
                }

            }
        }
        public void Clear()
        {
            TextCleared = Text;
            Text = "";
        }
        public void AppendText(string text)
        {
            Text = Text + text;
        }
        public void UndoClear()
        {
            Text = TextCleared;
            TextCleared = "";
        }
        public void CopyAll()
        {
            WinAPI.ClipBoard.ChangeContent(Text);
        }
        public void Paste()
        {
            AppendText(WinAPI.ClipBoard.GetContent());
        }
        public void CutAll()
        {
            WinAPI.ClipBoard.ChangeContent(Text);
            Clear();
        }
        public void Equals(string text)
        {
            Text = text;
        }
        #endregion
    }
}
